package cn.dkdoo.utils.templateDefault.redis;


import cn.dkdoo.utils.config.rollbackData.template.RollbackTemplateAbstract;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
 * @author nicky
 * @date 2021/5/14 上午11:25
 */
public class RollbackRedisTemplate<K, V> extends RollbackTemplateAbstract<RedisTemplate<K, V>> {
    protected String beanName;

    public RollbackRedisTemplate(StringRedisTemplate template, String beanName) {
        super((RedisTemplate<K, V>) template, beanName);
        this.beanName = beanName;
    }

    public RollbackRedisTemplate(StringRedisTemplate template) {
        super((RedisTemplate<K, V>) template);
        char[] cs = DefaultRedisTemplate.class.getSimpleName().toCharArray();
        //首字母大写到小写
        cs[0] += 32;
        this.beanName = String.valueOf(cs);
    }

    public void range(K key, long start, long end) {
        String methodName = "range";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(start);
        args.add(end);
        super.addRollbackData(methodName, args);
    }

    public void trim(K key, long start, long end) {
        String methodName = "trim";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(start);
        args.add(end);
        super.addRollbackData(methodName, args);
    }

    public void size(K key) {
        String methodName = "size";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        super.addRollbackData(methodName, args);
    }

    public void leftPush(K key, V value) {
        String methodName = "leftPush";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(value);
        super.addRollbackData(methodName, args);
    }

    public void leftPushAll(K key, V... values) {
        String methodName = "leftPushAll";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(values);
        super.addRollbackData(methodName, args);
    }

    public void leftPushAll(K key, Collection<V> values) {
        String methodName = "leftPushAll";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(values);
        super.addRollbackData(methodName, args);
    }

    public void leftPushIfPresent(K key, V value) {
        String methodName = "leftPushIfPresent";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(value);
        super.addRollbackData(methodName, args);
    }

    public void leftPush(K key, V pivot, V value) {
        String methodName = "leftPush";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(pivot);
        args.add(value);
        super.addRollbackData(methodName, args);
    }

    public void rightPush(K key, V value) {
        String methodName = "rightPush";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(value);
        super.addRollbackData(methodName, args);
    }

    public void rightPushAll(K key, V... values) {
        String methodName = "rightPushAll";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(values);
        super.addRollbackData(methodName, args);
    }

    public void rightPushAll(K key, Collection<V> values) {
        String methodName = "rightPushAll";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(values);
        super.addRollbackData(methodName, args);
    }

    public void rightPushIfPresent(K key, V value) {
        String methodName = "rightPushIfPresent";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(value);
        super.addRollbackData(methodName, args);
    }

    public void rightPush(K key, V pivot, V value) {
        String methodName = "rightPush";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(pivot);
        args.add(value);
        super.addRollbackData(methodName, args);
    }

    public void set(K key, long index, V value) {
        String methodName = "set";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(index);
        args.add(value);
        super.addRollbackData(methodName, args);
    }

    public void remove(K key, long i, Object value) {
        String methodName = "remove";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(i);
        args.add(value);
        super.addRollbackData(methodName, args);
    }

    public void index(K key, long index) {
        String methodName = "index";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(index);
        super.addRollbackData(methodName, args);
    }

    public void leftPop(K key) {
        String methodName = "leftPop";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        super.addRollbackData(methodName, args);
    }

    public void leftPop(K key, long timeout, TimeUnit unit) {
        String methodName = "leftPop";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(timeout);
        args.add(unit);
        super.addRollbackData(methodName, args);
    }

    public void rightPop(K key) {
        String methodName = "rightPop";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        super.addRollbackData(methodName, args);
    }

    public void rightPop(K key, long timeout, TimeUnit unit) {
        String methodName = "rightPop";
        Collection<Object> args = new ArrayList<>();
        args.add(key);
        args.add(timeout);
        args.add(unit);
        super.addRollbackData(methodName, args);
    }

    public void rightPopAndLeftPush(K sourceKey, K destinationKey) {
        String methodName = "rightPopAndLeftPush";
        Collection<Object> args = new ArrayList<>();
        args.add(sourceKey);
        args.add(destinationKey);
        super.addRollbackData(methodName, args);
    }

    public void rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) {
        String methodName = "rightPopAndLeftPush";
        Collection<Object> args = new ArrayList<>();
        args.add(sourceKey);
        args.add(destinationKey);
        args.add(timeout);
        args.add(unit);
        super.addRollbackData(methodName, args);
    }

}
